"""
数据模型定义
定义系统中使用的各种数据结构
"""

from dataclasses import dataclass, field
from datetime import datetime
from typing import Optional, List
from enum import Enum


class MessageType(Enum):
    """消息类型枚举"""
    DANMAKU = "danmaku"  # 弹幕
    GIFT = "gift"  # 礼物
    ENTER = "enter"  # 进入直播间
    SUPER_CHAT = "super_chat"  # 醒目留言
    GUARD_BUY = "guard_buy"  # 购买舰长


class EmotionType(Enum):
    """情感类型枚举"""
    NEUTRAL = "neutral"  # 中性
    HAPPY = "happy"  # 开心
    SURPRISED = "surprised"  # 惊讶
    CONFUSED = "confused"  # 疑惑
    SAD = "sad"  # 难过
    EXCITED = "excited"  # 兴奋


@dataclass
class DanmakuMessage:
    """弹幕消息数据模型"""
    message_id: str  # 消息唯一标识
    user_id: str  # 用户ID
    username: str  # 用户昵称
    content: str  # 弹幕内容
    timestamp: datetime = field(default_factory=datetime.now)  # 接收时间
    message_type: MessageType = MessageType.DANMAKU  # 消息类型
    is_vip: bool = False  # 是否VIP
    is_guard: bool = False  # 是否舰长
    priority: int = 0  # 优先级(数字越大优先级越高)

    def __post_init__(self):
        """设置优先级"""
        if self.is_guard:
            self.priority = 10
        elif self.is_vip:
            self.priority = 5
        else:
            self.priority = 0


@dataclass
class AIResponse:
    """AI回复数据模型"""
    text: str  # 回复文本
    emotion: EmotionType = EmotionType.NEUTRAL  # 情感标签
    timestamp: datetime = field(default_factory=datetime.now)  # 生成时间
    confidence: float = 1.0  # 置信度


@dataclass
class LipSyncData:
    """口型同步数据模型"""
    timestamp: float  # 时间点(秒)
    volume: float  # 音量强度(0-1)
    mouth_open: float  # 嘴巴张开度(0-1)
    phoneme: Optional[str] = None  # 音素类型


@dataclass
class Live2DParams:
    """Live2D参数数据模型"""
    mouth_open_y: float = 0.0  # 嘴巴张开(0-1)
    eye_l_open: float = 1.0  # 左眼睁开(0-1)
    eye_r_open: float = 1.0  # 右眼睁开(0-1)
    angle_x: float = 0.0  # 头部左右转动(-30到30)
    angle_y: float = 0.0  # 头部上下转动(-30到30)
    angle_z: float = 0.0  # 头部倾斜(-30到30)
    body_angle_x: float = 0.0  # 身体左右转动(-10到10)
    emotion: EmotionType = EmotionType.NEUTRAL  # 当前情感


@dataclass
class ConversationContext:
    """对话上下文数据模型"""
    conversation_id: str  # 会话ID
    history: List[dict] = field(default_factory=list)  # 消息历史
    user_info: dict = field(default_factory=dict)  # 用户信息缓存
    context_window_size: int = 10  # 上下文窗口大小

    def add_message(self, role: str, content: str):
        """添加消息到历史记录"""
        self.history.append({
            "role": role,
            "content": content,
            "timestamp": datetime.now()
        })
        # 保持窗口大小
        if len(self.history) > self.context_window_size:
            self.history.pop(0)

    def get_messages_for_api(self) -> List[dict]:
        """获取用于API调用的消息列表"""
        return [{"role": msg["role"], "content": msg["content"]} for msg in self.history]


@dataclass
class SystemStatus:
    """系统运行状态数据模型"""
    is_connected: bool = False  # 是否连接到B站
    current_emotion: EmotionType = EmotionType.NEUTRAL  # 当前情感状态
    is_speaking: bool = False  # 是否正在播放语音
    message_queue_length: int = 0  # 待处理消息数量
    total_messages_processed: int = 0  # 已处理消息总数
    start_time: datetime = field(default_factory=datetime.now)  # 启动时间
    last_update_time: datetime = field(default_factory=datetime.now)  # 最后更新时间

    @property
    def uptime(self) -> str:
        """运行时长"""
        delta = datetime.now() - self.start_time
        hours, remainder = divmod(int(delta.total_seconds()), 3600)
        minutes, seconds = divmod(remainder, 60)
        return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
